package com.ruoyi.flowable.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.flowable.domain.vo.FormVersionNameVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.flowable.domain.bo.FormBo;
import com.ruoyi.flowable.domain.vo.FormVo;
import com.ruoyi.flowable.domain.Form;
import com.ruoyi.flowable.mapper.FormMapper;
import com.ruoyi.flowable.service.IFormService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 流程表单Service业务层处理
 *
 * @author zxw
 * @date 2023-03-27
 */
@RequiredArgsConstructor
@Service
public class FormServiceImpl implements IFormService {

    private final FormMapper baseMapper;

    /**
     * 查询流程表单
     */
    @Override
    public FormVo queryById(Long formId){
        return baseMapper.selectVoById(formId);
    }

    /**
     * 查询流程表单列表
     */
    @Override
    public TableDataInfo<FormVo> queryPageList(FormBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Form> lqw = buildQueryWrapper(bo);
        Page<FormVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询流程表单列表
     */
    @Override
    public List<FormVo> queryList(FormBo bo) {
        LambdaQueryWrapper<Form> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Form> buildQueryWrapper(FormBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Form> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getFormName()), Form::getFormName, bo.getFormName());
        return lqw;
    }

    /**
     * 新增流程表单
     */
    @Override
    public Boolean insertByBo(FormBo bo) {
        Form add = BeanUtil.toBean(bo, Form.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFormId(add.getFormId());
        }
        return flag;
    }

    /**
     * 修改流程表单
     */
    @Override
    public Boolean updateByBo(FormBo bo) {
        Form update = BeanUtil.toBean(bo, Form.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Form entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除流程表单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<FormVersionNameVo> selectFormVersionList(Long formId) {
        return baseMapper.selectFormVersionList(formId);
    }
}
